package test;

import bean.Teacher;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import utils.MyBatisUtil;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @author 飞哥
 * @version V1.0
 * @package test
 * @date 2021/7/6 11:05
 */
public class CRUD_01 {
    @Test
    public void insert01() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession = factory.openSession();

        //插入的对象
        Teacher t1 = new Teacher();
        t1.setId(1006);
        t1.setName("果实");
        t1.setSex("男");
        t1.setEmail("guoshi@163.com");

        //返回值是插入的行数
        int rows = sqlSession.insert("dao.TeacherDao.insertTeacher", t1);

        //提交事务
        sqlSession.commit();

        System.out.println("插入了" + rows + "行");

        //记得要关闭sqlSession对象
        sqlSession.close();
    }

    @Test
    public void update01() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession = factory.openSession();

        Teacher t2 = new Teacher();
        t2.setId(1006);
        t2.setEmail("123456@qq.com");

        int rows = sqlSession.update("dao.TeacherDao.updateTeacher", t2);

        sqlSession.commit();
        System.out.println("修改的行数是：" + rows);

        sqlSession.close();
    }

    @Test
    public void delete01() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession = factory.openSession();

        Integer id = 1006;
        int rows = sqlSession.delete("dao.TeacherDao.deleteTeacher", id);

        sqlSession.commit();
        System.out.println("已经删除" + rows + "行");

        sqlSession.close();
    }

    @Test
    public void query01() throws IOException {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(Resources.getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession = factory.openSession();

        String sqlId = "dao.TeacherDao.queryTeachers";
        List<Teacher> list = sqlSession.selectList(sqlId);
        for (Teacher teacher : list) {
            System.out.println("教师的信息：" + teacher);
        }

        sqlSession.close();//关闭sqlSession对象
    }

    @Test
    public void query02(){
        String sqlId = "dao.TeacherDao.queryTeachers";
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        List<Teacher> list = sqlSession.selectList(sqlId);
        for (Teacher teacher : list) {
            System.out.println(teacher);
        }
    }
}
